package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    //--1--新增地址
    @Override
    public void add(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.add(addressBook);
    }

    //--2--查询当前登录用户的所有地址信息
    @Override
    public List<AddressBook> selectList() {
        return addressBookMapper.selectList(BaseContext.getCurrentId());
    }


    //--3-- 查询默认地址
    @Override
    public AddressBook selectdefault() {
        return addressBookMapper.selectdefault(BaseContext.getCurrentId());
    }

    //--4--根据id修改地址
    @Override
    public void updateByUserId(AddressBook addressBook) {
        //addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.updateByUserId(addressBook);
    }

    //--5--根据id删除地址
    @Override
    public void deleteById(Long id) {
        addressBookMapper.deleteById(id);
    }

    //--6--根据id查询地址
    @Override
    public AddressBook selectById(Long id) {
        return addressBookMapper.selectById(id);
    }


    //--7--设置默认地址
    //是否默认 0否 1是
    //private Integer isDefault;
    @Override
    @Transactional
    public void upDefault(Long id) {
        addressBookMapper.notListDefault(BaseContext.getCurrentId());  //当前用户所有地址（不默认）
        addressBookMapper.isOneDefault(id);      //当前地址（默认）
    }
}
